package com.rachel.studyapp.video;

import android.content.Context;
import android.util.TypedValue;

import com.google.android.exoplayer2.C;
import com.rachel.studyapp.R;

import java.util.Formatter;
import java.util.Locale;

/**
 * Created by zhengshaorui on 2017/8/27.
 */

public class CusVideoUtils {
    private static StringBuilder formatBuilder = new StringBuilder();
    private static Formatter formatter  = new Formatter(formatBuilder, Locale.getDefault());
    public static String stringForTime(long timeMs) {

        if (timeMs == C.TIME_UNSET) {
            timeMs = 0;
        }
        long totalSeconds = (timeMs + 500) / 1000;
        long seconds = totalSeconds % 60;
        long minutes = (totalSeconds / 60) % 60;
        long hours = totalSeconds / 3600;
        formatBuilder.setLength(0);
        return hours > 0 ? formatter.format("%d:%02d:%02d", hours, minutes, seconds).toString()
                : formatter.format("%02d:%02d", minutes, seconds).toString();
    }


    /**
     * 获取状态栏和actionbar 的高度
     * @param context
     * @return
     */
    public static int getTopHeight(Context context){
        TypedValue value = new TypedValue();
        if (context.getTheme().resolveAttribute(R.attr.actionBarSize,value,true)){
            int actionbarsize = TypedValue.complexToDimensionPixelSize(value.data,
                    context.getResources().getDisplayMetrics());
            return actionbarsize + getStatusBarHeight(context);
        }
        return -1;
    }

    /**
     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context){
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0){

            return  context.getResources().getDimensionPixelSize(resourceId);
        }
        return -1;
    }

}
